﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;
using Kooboo.CMS.SiteConverter.Providers;

namespace Kooboo.CMS.SiteConverter.Parser.Razor
{
    public class MetaParser : ParserBase, IParser<IMetaTag>
    {
        public static void FillResult(IMetaTag obj, IParseResult result)
        {
            if (result.Page.HtmlMeta == null)
                result.Page.HtmlMeta = new Kooboo.CMS.Sites.Models.HtmlMeta() { Customs = new Dictionary<string, string>() };

            var name = obj.Node.Attr("name");
            var content = obj.Node.Attr("content");
            //var httpName = obj.Meta.Attr("http-equiv");

            if (!string.IsNullOrEmpty(name))
            {
                var lowerName = name.ToLower();
                if (lowerName == "author")
                {
                    result.Page.HtmlMeta.Author = content;
                }
                else if (lowerName == "keywords")
                {
                    result.Page.HtmlMeta.Keywords = content;
                }
                else if (lowerName == "description")
                {
                    result.Page.HtmlMeta.Description = content;
                }
                else
                {
                    result.Page.HtmlMeta.Customs.Add(name, content);
                }

                // remove
                obj.Node.RemoveWithPrevWhitespace();
            }

            //TODO: no support http-equiv yet
        }

        #region IParser<IMetaTag> Members

        public void Parse(IMetaTag obj)
        {
            FillResult(obj, this.Context.Result);
        }

        #endregion
    }
}
